<template>
	<view>
		<baseHead :showReturn="true" title="感兴趣" bg="#fff" :showIcon="false" color="#000"
			:text_right="`批量操作${lenChecked == 0 ? '' : `(${lenChecked})`}`" @clickRight="clickRight" :right="true">
		</baseHead>

		<view v-if="datalist.length > 0">
			<scroll-view scroll-y :style="{'height':windowHeight-50-statusBarH+'px'}" :refresher-threshold="10"
				@scrolltolower="ReachBottom" :lower-threshold='50' refresher-enabled="true"
				:refresher-triggered="triggered" @refresherrefresh="onRefresh" @refresherrestore="onRestore"
				refresher-default-style="none" >
				<u-loading-icon :show="triggered" text="正在刷新" style="padding-top: 10rpx;"></u-loading-icon>
				<view v-for="(item, index) in datalist" @click="onItemClick(item, index)" style="padding: 0 20rpx;">
					<uni-swipe-action>
						<uni-swipe-action-item class="jiaonliguanli" @click="handleXingqu(item, index)"
							:class="{ active: isShow }" :right-options="options1" :autoClose="false" :disabled="isShow"
							:show="item.isOpened" @change="handgxqchange($event,index)" v-if="item.JobName">

							<view class="swipe-in-item">
								<view class="checklcsts" v-show="isShow">
									<view class="radio-check" @click="handleChangeCheck(item, index)">
										<u-icon class="iconcheck" v-if="item.checked == true" color="#1684FC" size="26"
											name="checkmark-circle-fill"></u-icon>
										<view v-if="item.checked == false" class="active"></view>
									</view>
								</view>
								<view class="view-layout" @click="JobDetails(item)">
									<view class="view-right">
										<view class="view-right-top">
											<view>
												<span class="name">{{ item.JobName }}</span>
											</view>
											<view>
												<span v-if="item.Negotiable" class="money">可面议</span>
												<span v-else-if="item.MaxWage<=3000" class="money">{{numberFormat(item.MaxWage)}}以下</span>
												<span v-else
													class="money">{{ numberFormat(item.MinWage, true) }}~{{ numberFormat(item.MaxWage)}}</span>

											</view>
										</view>
										<view class="view-info">
											<span class="spantext">{{ item.EducationDesc }}</span>
											<span class="spantext" v-if="item.Graduate">应届生</span>
											<span class="spantext" v-else-if="item.Student">在校生</span>
											<span class="spantext" v-else-if="item.MaxExperience>10">10年以上经验</span>
											<span class="spantext" v-else-if="item.MaxExperience<=1">1年以内经验</span>
											<span class="spantext"
												v-else>{{ item.MinExperience }}-{{ item.MaxExperience }}年经验</span>
											<span class="spantext" v-if="item.WorkType == 0">全职</span>
											<span class="spantext" v-else-if="item.WorkType == 1">兼职</span>
											<span class="spantext" v-else>实习</span>
											<span class="spantext">招{{ item.Amount }}人</span>
										</view>
										<view class="view-info " style="display: flex;">
											<u-tag :text="s.FullName" size="mini" borderColor="#F7F7F7" color="#666666"
												bgColor="#F7F7F7" plain v-for="(s,index) in item.Tags" :key="index"
												style="margin: 0rpx 15rpx 0 0"></u-tag>
										</view>

										<view class="view-info cop">
											<view class="cop-name">
												<image class="logo" :src="imgStr(item)"></image>
												<view class="stateCla">
													<view v-if="item.UserOnlineInfo ">
														<view class="onlineStateCla"
															v-if="item.UserOnlineInfo.State == true"></view>
													</view>
													<view class="noOnlineStateCla" v-else></view>
												</view>
												<span class="textspan">{{ item.CorpName }}</span>
											</view>
											<view class="pcaaddress">
												{{ item.PcaAddress }}
											</view>
										</view>
									</view>
								</view>
							</view>
						</uni-swipe-action-item>
					</uni-swipe-action>
				</view>
				<u-loadmore :status="status" />
			</scroll-view>



			<view v-show="isShow" style="height: 100rpx;"></view>


		</view>

		<view v-else>
			<u-empty mode="data" icon="/static/recruit/index/qsy.png" width="350" marginTop="150">
			</u-empty>
		</view>

		<view class="fiexd" v-if="isShow">
			<view class="fiexd-cont">
				<view>
					<u-checkbox-group v-model="checkedAll" @change="handleAllCheck">
						<u-checkbox size="21" shape="circle" :name="value[0].name" label="全选"></u-checkbox>
					</u-checkbox-group>
				</view>
				<view class="btnbox2" @click="handleDel">
					<view class="btntext">删除</view>

				</view>
			</view>
		</view>

	</view>
</template>
<script>
	import baseHead from "@/pages/component/baseHead.vue";
	export default {
		filters: {
			userNums(num) {
				return num < 10000 ? num : (num / 1000).toFixed(1) + "k";
			}
		},
		data() {
			return {
				URL: this.$config.domain.staticUrl,
				checkedAll: [],
				value: [{
					name: "全选",
					disabled: false,
				}],
				options1: [{
					text: "删除",
					style: {
						backgroundColor: "#F35370",
					},
				}],
				checked: 0,
				datalist: [],
				obj: {
					PageIndex: 1,
					PageSize: 10,
				},
				status: "loadmore",

				lenChecked: 0,
				isShow: false,
				statusBarH: '',
				windowHeight: '',
				windowWidth: '',
				triggered: false,
				
			}
		},
		watch: {
			datalist: {
				handler(nVal) {
					let arr = this.datalist.filter((item) => {
						return item.checked == true;
					});
					if (arr.length > 0) {
						this.lenChecked = arr.length;
					} else {
						this.lenChecked = 0;
					}
					if (arr.length == this.datalist.length && arr.length != 0) {
						this.checkedAll = ["全选"];
					} else {
						this.checkedAll = [];
					}
				},
				deep: true,
			},
			isOpened: {
				handler(nVal) {
					console.log(nVal);
				}
			}
		},
		//子组件
		components: {
			baseHead,
		},
		
		onShow() {
			this.datalist = [];
			const app = getApp()
			this.statusBarH = app.globalData.statusBarH //状态栏高度
			this.windowHeight = app.globalData.phoneInformation.windowHeight //屏幕高度
			this.windowWidth = app.globalData.phoneInformation.windowWidth
			this.listdata();
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.obj = {
				PageIndex: 1,
				PageSize: 10,
			};
			this.listdata();
			uni.stopPullDownRefresh(); //停止刷新
		},
		// 下拉刷新
		onReachBottom() {
			console.log("懒加载");
			// 加载前值为loadmore，加载中为loading，没有数据为nomore
			if (this.status != "nomore") {
				this.status = "loading";
				this.listdata();
			}
		},
		// 计算属性
		computed: {},
		// 计算方法
		methods: {
			ReachBottom() {
				// 加载前值为loadmore，加载中为loading，没有数据为nomore
				if (this.status != "nomore") {
					this.status = "loading";
					this.listdata();
				}
			},
			// 下拉刷新
			onRefresh() {
				this.triggered = true;
				setTimeout(() => {
					this.triggered = false;
					this.obj = {
						PageIndex: 1,
						PageSize: 10,
						KeyWord: "",
					};
					this.datalist = []
					this.listdata();

				}, 2000)
			},
			// 下拉复位
			onRestore() {
				this.triggered = false; // 需要重置
			},
			imgStr(item) {
				if (item.Corp.LogoPath) {
					return this.URL + item.Corp.LogoPath
				} else {
					return '/static/assets/images/default-corp-logo.png'
				}
			},

			// 删除按钮
			handleDel() {
				let arr = this.datalist.filter((item) => {
					return item.checked == true;
				});
				let resumeArr = arr.map((item) => {
					return item.Id;
				});

				let obj = {
					Ids: resumeArr,
				};
				this.$http.huirc.collectdel(obj).then((res) => {
					if (res.Success) {
						this.$util.toast(res.Message);
						this.isShow = false;
						this.datalist = [];
						this.obj.PageIndex = 1;
						this.obj.PageSize = 10;
						this.listdata();
					}
				});
			},
			handleAllCheck(e) {
				if (e.length > 0) {
					this.datalist.forEach((item) => {
						item.checked = true;
					});
				} else {
					this.datalist.forEach((item) => {
						item.checked = false;
					});
				}
				let arr = this.datalist.filter((item) => {
					return item.checked == true;
				});
				this.lenChecked = arr.length;
			},
			// 跳转详情
			JobDetails(item) {
				console.log(item);
				let id = item.Id;
				if (this.isShow) {
					item.checked = !item.checked
				} else {
					uni.navigateTo({
						url: `/pages/views/packageA/type-jobhunting/search/JobDetails?id=` + id,
					});
				}
			},
			onItemClick(item, index) {
				console.log(item, index);
				//判断当前item侧滑展开状态，若为关闭，判断其他项的展开状态后进行点击事件；若为开启，则只做关闭操作
				if (item.isOpened == "none") {
					var flag = false
					var openPosition = -1
					for (var i = 0; i < this.datalist.length; i++) {
						if (this.datalist[i].isOpened != "none") {
							flag = true
							openPosition = i;
						}
					}
					//判断是否有其它侧滑展开项，若有，则关闭，若无，直接进行点击事件逻辑
					if (flag) {
						this.datalist[openPosition].isOpened = "none"
					}
					//点击事件
				} else {
					item.isOpened = "none"
				}

			},
			handgxqchange(e, index) {
				console.log(e, index);
				if (e == 'left' || e == 'right') {
					for (var i = 0; i < this.datalist.length; i++) {
						if (i == index) {
							// 将对应下表的订单isOpened
							this.datalist[i].isOpened = 'right'
						} else {
							// 其他设置为关闭状态
							this.datalist[i].isOpened = 'none'
						}
					}
				}
			},
			handleXingqu(val, index) {
				let obj = {
					Ids: [val.Id],
				};
				this.$http.huirc.collectdel(obj).then((res) => {
					if (res.Success) {
						this.obj.PageIndex = 1;
						this.datalist = [];
						this.listdata()
						this.$util.toast(res.Message);
						uni.$emit('jobrefresh', true)
					}
				});
			},

			numberFormat(num, val) {
				let newNum = num / 1000;
				newNum = newNum.toString();
				const index = newNum.indexOf(".");
				if (index !== -1) {
					if (!val) {
						return parseFloat(newNum).toFixed(1) + "k";
					} else {
						return parseFloat(newNum).toFixed(1);
					}
				} else {
					if (!val) {
						return parseFloat(newNum) + "k";
					} else {
						return parseFloat(newNum);
					}
				}
			},
			handleChangeCheck(val, index) {
				console.log(val, index);
				this.datalist[index].checked = !this.datalist[index].checked;
				console.log(this.datalist);
			},
			clickRight() {
				this.isShow = !this.isShow;
				this.threshold = 0
				this.isOpened = 'none'
				if (!this.isShow) {
					this.lenChecked = 0;
					this.datalist = this.datalist.map((item) => {
						return {
							...item,
							checked: false
						}
					})
				} else {
					this.datalist = this.datalist.map((item) => {
						return {
							...item,
							isOpened: 'none'
						};
					});
				}
			},
			listdata(val) {
				let obj = {
					PageIndex: this.obj.PageIndex,
					PageSize: this.obj.PageSize,
				};
				this.$http.huirc.collectlist(obj).then((res) => {
					if (res.Result) {
						if (this.obj.PageIndex == 1 && res.Result.Items.length == 0) {
							this.datalist = [];
						} else {
							if (res.Result.Items.length == 0) {
								// 没有数据----
								this.status = "nomore";
								return;
							}
							this.datalist = this.datalist
								.concat(res.Result.Items)
								.map((item) => {
									return {
										...item,
										checked: false,
										isOpened: 'none'
									}
								})
						}
						if (res.Result.Items.length >= this.obj.PageSize) {
							this.obj.PageIndex++;
							// 表示还有数据
						} else {
							//没有更多数据了
							this.status = "nomore";
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.jiaonliguanli {
		background-color: #ffffff;
		margin-top: 20rpx;
		border-radius: 20rpx;

		.swipe-in-item {
			padding: 0 10rpx;
			padding-right: 0;
			display: flex;
			width: 100%;
			flex: 1;

			.checklcsts {
				display: flex;
				align-items: center;

				.radio-check {
					display: flex;
					align-items: center;
					justify-content: center;
					width: 43rpx;
					height: 40rpx;
					margin-right: 15rpx;

					.iconcheck {
						span {
							width: 43rpx;
							height: 40rpx;
							overflow: hidden;
						}
					}

					.active {
						width: 43rpx;
						height: 40rpx;
						border-radius: 50%;
						border: 2rpx solid #666666;
					}
				}
			}

			.view-layout {
				width: 90%;
			}
		}

		&.active {
			.swipe-in-item {
				background-color: #f9f9f9;
			}

			.view-layout {
				background-color: #ffffff;
				padding: 20rpx;
			}
		}

		.view-layout {
			display: flex;
			justify-content: space-between;
			padding: 20rpx 0;

			.view-right {
				margin-left: 25rpx;
				flex: 1;

				.view-right-top {
					width: 100%;
					display: flex;
					justify-content: space-between;
					border-radius: 4rpx;

					.name {
						font-size: 36rpx;
						font-weight: bold;
						color: #000000;
					}

					.money {
						margin-left: 20rpx;
						font-size: 34rpx;
						font-weight: 400;
						color: #F35370;
					}
				}

				.view-info {
					margin: 25rpx 0;

					.spantext {
						font-size: 26rpx;
						font-weight: 400;
						color: #666666;
					}

					.cop-name {
						display: flex;
						align-items: center;
						position: relative;

						.logo {
							flex-shrink: 0;
							width: 42rpx;
							height: 42rpx;
							background: #FFFFFF;
							border-radius: 8rpx;
							opacity: 1;
							border: 1rpx solid rgba(112, 112, 112, 0.2);
						}

						.textspan {
							margin-left: 20rpx;
							font-size: 26rpx;
							font-weight: 500;
							color: #272636;
						}

						.stateCla {
							position: absolute;
							top: 38rpx;
							left: 38rpx;

							.onlineStateCla {
								width: 12rpx;
								height: 12rpx;
								background-color: #14C88B;
								border-radius: 100px;
								float: right;
							}

							.noOnlineStateCla {
								width: 12rpx;
								height: 12rpx;
								background-color: #b2b6bf;
								border-radius: 100px;
								float: right;
							}
						}
					}

					.pcaaddress {
						color: #999999;
						font-size: 24rpx;
						font-weight: 400;
					}

					&.cop {
						display: flex;
						align-items: center;
						justify-content: space-between;
					}

					span {
						font-size: 24rpx;
						color: #313131;
						padding: 0 12rpx;
						line-height: 24rpx;
						margin-right: 20rpx;

						&:first-child {
							padding-left: 0;
						}

						&:last-child {
							border-right: 0;
						}
					}


					&.flex {
						display: flex;
						justify-content: space-between;
					}
				}
			}
		}

	}

	.fiexd {
		position: fixed;
		bottom: 0;
		width: 100%;
		background-color: #fff;
		box-shadow: 0px 0px 5px 1px rgba(131, 140, 149, 0.14);
		z-index: 99999;

		.grid-text {
			font-size: 28rpx;
			line-height: 98rpx;
		}

		.fiexd-cont {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 20rpx 20rpx;

			.btnbox2 {
				padding: 16rpx 50rpx;
				background: #3476FE;
				border-radius: 10rpx;
				opacity: 1;
				display: flex;
				align-items: center;
				justify-content: center;

				.btntext {
					font-size: 26rpx;
					font-weight: 400;
					color: #FFFFFF;
				}
			}
		}
	}
</style>